package com.goodving.guli.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.lua.C1226h;
import com.goodving.guli.bt.lua.DaoManager;
import com.goodving.guli.bt.lua.LuaAction;
import com.goodving.guli.bt.lua.LuaEvent;
import com.goodving.guli.bt.model.LuaItem;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;
import de.greenrobot.dao.query.WhereCondition;
import de.greenrobot.gulidao.LuaList;
import de.greenrobot.gulidao.LuaListDao;
import de.greenrobot.gulidao.LuaListDao.Properties;

/**
 * Created by john on 2015/12/1.
 */
public class LuaListAdapter extends BaseAdapter {
    private static final String DISABLE_TAG = "_DISABLE";
    private static final String TAG = "LuaListAdapter";
    private static Boolean isFirstTimeBoolean;
    private Animation animT2B;
    private int fakeListCount;
    private List<C1437d> infoList;
    private Context mContext;
//    private PartnerDataManager mServiceManager;
    private int newItemCount;
    private TranslateAnimation taBlow;
    private TranslateAnimation taLeft;
    private TranslateAnimation taRight;
    private TranslateAnimation taTop;

    class C1437d {
        LuaList luaList;
        Boolean f7324b;
        final /* synthetic */ LuaListAdapter adapter;

        C1437d(LuaListAdapter luaListAdapter) {
            this.adapter = luaListAdapter;
            this.f7324b = Boolean.valueOf(false);
        }
    }

    public class MyComparator implements Comparator<LuaList> {
        public int compare(LuaList luaList, LuaList luaList2) {
            String right = luaList2.getRight();
            String right2 = luaList.getRight();
            return (right == null && right2 == null) ? 0 : (right != null || right2 == null) ? (right == null || right2 != null) ? right.length() > right2.length() ? 1 : right.length() >= right2.length() ? right.compareTo(right2) : -1 : 1 : -1;
        }
    }

    static {
        isFirstTimeBoolean = Boolean.valueOf(true);
    }

    public LuaListAdapter(Context context, List<LuaList> list) {
        this.mContext = null;
        this.fakeListCount = 0;
        this.infoList = null;
        this.newItemCount = 0;
        this.mContext = context;
        isFirstTimeBoolean = Boolean.valueOf(true);
//        this.mServiceManager = new PartnerDataManager(context);
        getNewList(list);
        InitAnim();
    }

    private void InitAnim() {
        this.taLeft = new TranslateAnimation(2, 1.0f, 2, 0.0f, 2, 0.0f, 2, 0.0f);
        this.taRight = new TranslateAnimation(2, -1.0f, 2, 0.0f, 2, 0.0f, 2, 0.0f);
        this.taTop = new TranslateAnimation(2, 0.0f, 2, 0.0f, 2, 1.0f, 2, 0.0f);
        this.taBlow = new TranslateAnimation(2, 0.0f, 2, 0.0f, 2, -1.0f, 2, 0.0f);
        this.taLeft.setDuration(1000);
        this.taRight.setDuration(1000);
        this.taTop.setDuration(1000);
        this.taBlow.setDuration(1000);
        this.animT2B = AnimationUtils.loadAnimation(this.mContext, R.anim.list_anim_slide_t2b);
    }

    private String getDate() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    private void getNewList(List<LuaList> list) {
        this.newItemCount = 0;
        if (list == null || list.size() == 0) {
            this.infoList = null;
            return;
        }
        List arrayList = new ArrayList();
        int i;
        C1437d c1437d;
        if (this.infoList == null || this.infoList.size() == 0) {
            for (i = 0; i < list.size(); i++) {
                c1437d = new C1437d(this);
                c1437d.luaList = (LuaList) list.get(i);
                c1437d.f7324b = Boolean.valueOf(false);
                arrayList.add(c1437d);
            }
            this.newItemCount = arrayList.size();
        } else {
            for (i = 0; i < list.size(); i++) {
                c1437d = new C1437d(this);
                c1437d.luaList = (LuaList) list.get(i);
                if (judgeIsNew(c1437d.luaList).booleanValue()) {
                    this.newItemCount++;
                    c1437d.f7324b = Boolean.valueOf(true);
                } else {
                    c1437d.f7324b = Boolean.valueOf(false);
                }
                arrayList.add(c1437d);
            }
        }
        this.infoList = arrayList;
    }

    private Boolean judgeIsNew(LuaList luaList) {
        for (int i = 0; i < this.infoList.size(); i++) {
            if (luaList.getId() == ((C1437d) this.infoList.get(i)).luaList.getId()) {
                return ((C1437d) this.infoList.get(i)).f7324b;
            }
        }
        return Boolean.valueOf(true);
    }

    public int getCount() {
        if (this.infoList == null || this.infoList.size() == 0) {
            this.fakeListCount = 3;
            return this.fakeListCount;
        }
        this.fakeListCount = 0;
        return this.infoList.size();
    }

    public Object getItem(int i) {
        return Integer.valueOf(i);
    }

    public long getItemId(int i) {
        return (long) i;
    }

    public int getNewItemCount() {
        if (!isFirstTimeBoolean.booleanValue()) {
            return this.newItemCount;
        }
        isFirstTimeBoolean = Boolean.valueOf(false);
        return 0;
    }

    class LuaListItem { // C1438e
        TextView textViewA;
        TextView textViewB;
        ImageView imageViewA;
        ImageView imageViewB;
        final /* synthetic */ LuaListAdapter adapter;

        LuaListItem(LuaListAdapter luaListAdapter) {
            this.adapter = luaListAdapter;
            this.textViewA = null;
            this.textViewB = null;
        }
    }

    class C1436c implements View.OnClickListener {
        final /* synthetic */ LuaList f7321a;
        final /* synthetic */ LuaListAdapter f7322b;

        C1436c(LuaListAdapter luaListAdapter, LuaList luaList) {
            this.f7322b = luaListAdapter;
            this.f7321a = luaList;
        }

        public void onClick(View view) {
            LuaList luaList = (LuaList) ((LuaListItem) view.getTag()).textViewA.getTag();
            C0668a.m8128a(this.f7322b.mContext, C0668a.da, luaList.getType());
            GuliLog.tracePrintInfo("LuaListAdapter", "upload clicked type to server " + luaList.getType());
            String luaActionScript = luaList.getLuaActionScript();
            if (luaActionScript != null && luaActionScript.length() >= 5) {
                if (luaActionScript.contains("weight_history_result")) {
                    C0668a.m8126a(this.f7322b.mContext, C0668a.fL);
                }
                try {
//                    if (PartnerDataManager.LUA_SERVICE_RIGHT.equals(this.f7321a.getRight())) {
//                        if (this.f7321a.getType() != null) {
//                            C0668a.m8126a(this.f7322b.mContext, C0708b.bH + this.f7321a.getType());
//                        }
//                        Partner serviceById = this.f7322b.mServiceManager.getServiceById(this.f7321a.getType());
//                        if (serviceById != null) {
//                            this.f7322b.mContext.startActivity(WebActivity.buildIntent(this.f7322b.mContext, serviceById));
//                            return;
//                        }
//                    }
                    if (luaActionScript.startsWith("http://")) {
                        GuliLog.tracePrintError("chenee", "should not use http:// use script directly instead!!(script:" + luaActionScript + ")");
                    } else if (luaActionScript.startsWith("file://")) {
                        GuliLog.tracePrintError("chenee", "should not use file:// use script directly instead!!(script:" + luaActionScript + ")");
                    } else {
                        LuaAction.getInstance(this.f7322b.mContext).doLuaAction(luaActionScript);
                    }
                } catch (Exception e) {
                    GuliLog.tracePrintError("chenee", e.getMessage());
                }
            }
        }
    }

    class C1435b implements ImageLoadingListener {
        final /* synthetic */ LuaListAdapter f7320a;

        C1435b(LuaListAdapter luaListAdapter) {
            this.f7320a = luaListAdapter;
        }

        public void onLoadingStarted(String str, View view) {
            GuliLog.tracePrintInfo("LuaListAdapter", "onLoadingStarted");
        }

        public void onLoadingComplete(String str, View view, Bitmap bitmap) {
        }

        public void onLoadingFailed(String str, View view,  FailReason c3051b) { //C3051b
            GuliLog.tracePrintInfo("LuaListAdapter", "onLoadingFailed");
            ((ImageView) view).setImageResource(R.drawable.game_banner_icon);
        }

        public void onLoadingCancelled(String str, View view) {
        }
    }


    public View getView(int i, View view, ViewGroup viewGroup) {
        LuaListItem listItem;
        String str = null;
        if (view == null) {
            LuaListItem newItem = new LuaListItem(this);
            view = LayoutInflater.from(this.mContext).inflate(R.layout.lua_list_item, null);
            newItem.textViewA = (TextView) view.findViewById(R.id.textView1);
            newItem.textViewB = (TextView) view.findViewById(R.id.textView2);
            newItem.imageViewA = (ImageView) view.findViewById(R.id.lua_list_item_icon);
            newItem.imageViewB = (ImageView) view.findViewById(R.id.imageView1);
            view.setTag(newItem);
            if (C0804t.m8835a()) {
                MarginLayoutParams marginLayoutParams = (MarginLayoutParams) newItem.textViewA.getLayoutParams();
                marginLayoutParams.bottomMargin = (int) C0804t.m8825a(this.mContext, 4.0f);
                newItem.textViewA.setTextSize(13.5f);
                newItem.textViewA.setLayoutParams(marginLayoutParams);
                newItem.textViewB.setTextSize(11.0f);
                listItem = newItem;
            } else {
                listItem = newItem;
            }
        } else {
            listItem = (LuaListItem) view.getTag();
        }
        if (this.fakeListCount > 0) {
            listItem.imageViewB.setVisibility(View.INVISIBLE);
        } else {
            C1437d c1437d = (C1437d) this.infoList.get(i);
            LuaList luaList = c1437d.luaList;
            if (luaList.getType().contains(DISABLE_TAG)) {
                listItem.imageViewB.setVisibility(View.INVISIBLE);
            } else {
                listItem.imageViewB.setVisibility(View.VISIBLE);
            }
            String jsonString = luaList.getJsonString();
            int color = this.mContext.getResources().getColor(R.color.main_ui_title_color);
            try {
                JSONObject jSONObject = new JSONObject(jsonString);
                color = jSONObject.optInt(LuaItem.JSON_TXT_COLOR);
                str = jSONObject.optString(LuaItem.JSON_ICON_URI);
            } catch (JSONException e) {
                color = color;
            } catch (NullPointerException e2) {
                color = color;
            }
            GuliLog.tracePrintInfo(TAG, "iconUri = " + str + " txtColor = " + color);
            if (TextUtils.isEmpty(str)) {
                listItem.imageViewA.setVisibility(View.GONE);
                ((LayoutParams) ((LinearLayout) view.findViewById(R.id.linearLayout1)).getLayoutParams()).leftMargin = (int) this.mContext.getResources().getDimension(R.dimen.mili_margin);
            } else {
                listItem.imageViewA.setVisibility(View.VISIBLE);
                ((LayoutParams) ((LinearLayout) view.findViewById(R.id.linearLayout1)).getLayoutParams()).leftMargin = 0;
                C1226h.m11372a().m11378a(str, listItem.imageViewA, new C1435b(this));
            }
            listItem.textViewA.setTextColor(0xcc000000 | color);
            //listItem.textViewB.setTextColor((0x88000000 & color) | 0xffffff);
            listItem.textViewB.setTextColor(0x88000000 & color);
            view.setOnClickListener(new C1436c(this, luaList));
            listItem.textViewA.setText(luaList.getText1());
            CharSequence text2 = luaList.getText2();
            if (text2 == null || text2.length() == 0) {
                listItem.textViewB.setVisibility(View.GONE);
            } else {
                listItem.textViewB.setVisibility(View.VISIBLE);
                listItem.textViewB.setText(text2);
            }
            view.setTag(listItem);
            listItem.textViewA.setTag(luaList);
            if (c1437d.f7324b.booleanValue()) {
                view.startAnimation(this.animT2B);
            }
            c1437d.f7324b = Boolean.valueOf(false);
        }
        return view;
    }

    public void refExpTimeDB(LuaList luaList, long j) {
        LuaItem luaItem = new LuaItem();
        luaItem.setStype(luaList.getType());
        luaItem.setT1(luaList.getText1());
        luaItem.setT2(luaList.getText2());
        luaItem.setExpire(j);
        luaItem.setScript(luaList.getLuaActionScript());
        luaItem.setRight(luaList.getRight());
        luaItem.setStyleJson(luaList.getJsonString());
        LuaEvent.getInstance(this.mContext).showLuaItem(luaItem);
        GuliLog.tracePrintInfo(TAG, "refExpTimeDB");
    }

    public void refreshList() {
        LuaListDao b = DaoManager.getInstance().getLuaListDao();
        List<LuaList> list = b.queryBuilder().where(LuaListDao.Properties.ExpireTime.isNotNull(), LuaListDao.Properties.ExpireTime.notEq(C3657a.f18328f)).orderDesc(Properties.Id).list();
        if (!(list == null || list.size() == 0)) {
            for (LuaList luaList : list) {
                String expireTime = luaList.getExpireTime();
                if (expireTime.contains("-")) {
                    String[] split = expireTime.split("-");
                    Calendar instance = Calendar.getInstance();
                    instance.set(Integer.valueOf(split[0]).intValue(), Integer.valueOf(split[1]).intValue() - 1, Integer.valueOf(split[2]).intValue());
                    instance.set(Calendar.HOUR_OF_DAY, 23);
                    instance.set(Calendar.MINUTE, 59);
                    instance.set(Calendar.SECOND, 59);
                    GuliLog.tracePrintInfo(TAG, "old Lua-expire: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(instance.getTimeInMillis())));
                    refExpTimeDB(luaList, instance.getTimeInMillis() / 1000);
                }
            }
        }
        String date = getDate();
        long l = System.currentTimeMillis() / 1000L;
        GuliLog.tracePrintInfo(TAG, "===curTime: " + l);
        QueryBuilder queryBuilder = b.queryBuilder();
        List list2 = queryBuilder.whereOr(queryBuilder.and(Properties.Date.eq(date), Properties.ExpireTime.eq(C3657a.f18328f), new WhereCondition[0]),
                queryBuilder.and(Properties.Date.eq(date), Properties.ExpireTime.isNull(), new WhereCondition[0]),
                queryBuilder.and(Properties.ExpireTime.isNotNull(), Properties.ExpireTime.ge(Long.valueOf(l)),
                        new WhereCondition[0])).orderDesc(Properties.Id).list();
        Collections.sort(list2, new MyComparator());
        setList(list2);
    }

    public void setFlagsToFalse() {
        for (int i = 0; i < this.infoList.size(); i++) {
            ((C1437d) this.infoList.get(i)).f7324b = Boolean.valueOf(false);
        }
    }

    public void setList(List<LuaList> list) {
        if (list != null) {
            getNewList(list);
            notifyDataSetChanged();
        }
    }
}
